package com.lhdx.practical.dao;

import com.lhdx.practical.bean.Person;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface PersonDao extends CrudRepository<Person,Long> {
    void deleteById(long id);
    Person findById(long id);

    Person findByUsernameAndPassword(String username,String password);

    List<Person> findByDepartid(long departid);

    List<Person> findAllByTypeAndGradeid(int type,long gradeid);

    List<Person> findByGroupidOrderByJointimeAsc(long groupid);

    Page<Person> findAll(Specification<Person> spec, Pageable pageable);
    /**
     * 修改登录时间
     * @param id
     * @param logintime
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update person set logintime = ?2 where id = ?1",nativeQuery = true)
    void updateLoginTime(long id,long logintime);

    /**
     * 修改密码
     * @param id
     * @param password
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update person set password = ?2 where id = ?1",nativeQuery = true)
    void updatePassword(long id,String password);

    /**
     * 修改头像
     * @param id
     * @param imgface
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update person set imgface = ?2 where id = ?1",nativeQuery = true)
    void updateFaceImg(long id,String imgface);

    /**
     * 修改是否加入小组判断
     * @param id
     * @param isjoin
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update person set isjoin = ?2 where id = ?1",nativeQuery = true)
    void updateIsJoin(long id,int isjoin);


    /**
     * 修改小组信息 绑定小组ID
     * @param id
     * @param groupid
     */
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update person set groupid = ?2,groupname=?3,jointime=?4 where id = ?1",nativeQuery = true)
    void updateGroupid(long id,long groupid,String groupname,long datetime);


    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update person set groupid = 0,jointime=0,isjoin=0 where groupid = ?1",nativeQuery = true)
    void updateCancelGroupid(long groupid);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update person set profile = ?2 where id = ?1",nativeQuery = true)
    void updateProfile(long id,String profile);

    int countByRealname(String realname);

    int countByUserno(String userno);

    int countAllByDepartid(long departid);
}
